package net.zhengxinyun.performance.service.examNew;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.dto.examDTO.ExamDTO;
import net.zhengxinyun.performance.dto.examDTO.OptionDTO;
import net.zhengxinyun.performance.entity.exam.*;
import net.zhengxinyun.performance.mapper.examNew.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.management.OperatingSystemMXBean;
import java.util.ArrayList;
import java.util.List;

/**
 * Date: 2020/3/13 14:21
 * 三种问卷增删改查的基本操作
 */

@Service
public class PaperServiceImpl  implements PaperService{

    @Autowired
    private WorkPaperEntityMapper workMapper;
    @Autowired
    private DeptPaperEntityMapper deptMapper;
    @Autowired
    private GeneralPaperEntityMapper generalMapper;

    @Autowired
    private ExamResultEntityMapper examResultEntityMapper;

    @Autowired
    private ExamUserStatueMapper summintMapper;
    @Autowired
    private PyInqObjEntityMapper pyobjMapper;

    /**
     * 预览问卷，
     * @param examType
     * @param questionnaireId
     * @return
     */
    @Override
    public List<ExamDTO> priviewPages(String examType, Long questionnaireId) {

        List<ExamDTO> examDTOS=new ArrayList<>();
        switch (examType){
            case "DW":
               // OptionDTO opts = new OptionDTO("很满意","较满意","满意","一般","差","不了解");

                examDTOS = deptMapper.selectListPages(questionnaireId);

                examDTOS.forEach(examDTO -> examDTO.setOpA("很满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpB("较满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpC("满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpD("一般"));
                examDTOS.forEach(examDTO -> examDTO.setOpE("差"));
                examDTOS.forEach(examDTO -> examDTO.setOpF("不了解"));

                break;

            case "GZ":
                examDTOS = workMapper.selectListWorks(questionnaireId);
               // OptionDTO work = new OptionDTO("很满意","较满意","满意","一般","差","不了解");
                examDTOS.forEach(examDTO -> examDTO.setOpA("很满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpB("较满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpC("满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpD("一般"));
                examDTOS.forEach(examDTO -> examDTO.setOpE("差"));
                examDTOS.forEach(examDTO -> examDTO.setOpF("不了解"));
                break;
            case "YB":
                examDTOS = generalMapper.selectGenersLists(questionnaireId);



            default:
                return null;
        }

        return examDTOS;
    }


    @Override
    public List<ExamDTO> getCachePages(String examType, Long questionaireId, Long pyId, Long userId) {
        List<ExamDTO> examDTOS=new ArrayList<>();
        switch (examType){
            case "DW":
                // OptionDTO opts = new OptionDTO("很满意","较满意","满意","一般","差","不了解");

                //examDTOS = deptMapper.selectListPages(questionnaireId);
                examDTOS= deptMapper.selectCacheList(questionaireId,userId,pyId);
                examDTOS.forEach(examDTO -> examDTO.setOpA("很满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpB("较满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpC("满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpD("一般"));
                examDTOS.forEach(examDTO -> examDTO.setOpE("差"));
                examDTOS.forEach(examDTO -> examDTO.setOpF("不了解"));

                break;

            case "GZ":
                examDTOS = workMapper.selectCacheList(questionaireId, userId, pyId);
                //  examDTOS = workMapper.selectListWorks(questionnaireId);
                // OptionDTO work = new OptionDTO("很满意","较满意","满意","一般","差","不了解");
                examDTOS.forEach(examDTO -> examDTO.setOpA("很满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpB("较满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpC("满意"));
                examDTOS.forEach(examDTO -> examDTO.setOpD("一般"));
                examDTOS.forEach(examDTO -> examDTO.setOpE("差"));
                examDTOS.forEach(examDTO -> examDTO.setOpF("不了解"));
                break;
            case "YB":
               // examDTOS = generalMapper.selectGenersLists(questionnaireId);
                examDTOS = generalMapper.selectCacheList(questionaireId,userId,pyId);


            default:
                return null;
        }

        return examDTOS;


    }

    /**
     * 答题缓存表
     */
    @Override
    public int  cacheResult(Long examId, Long questionnaireId, Long userId,Long pyId,String inqSource,String examType,String resultOpt) {

        int i =0;
        ExamDTO dto = new ExamDTO();
        dto.setExamId(examId);
        dto.setQuestionnaireId(questionnaireId);
        dto.setUserId(userId);
        dto.setPyId(pyId);
        dto.setInquireSource(inqSource);
        dto.setExamType(examType);
        List<ExamResultEntity> listResults = examResultEntityMapper.selectRecords(dto);
        if(ObjectUtils.isBlank(listResults)){
            ExamResultEntity record = new ExamResultEntity();
            record.setInquireSource(inqSource);
            record.setPyId(pyId);
            record.setUserId(userId);
            record.setQuestionnaireId(questionnaireId);
            record.setExamType(examType);
            record.setResultOpt(resultOpt);
            record.setExamId(examId);
            i= examResultEntityMapper.insert(record);

        }else {

            ExamResultEntity record = new ExamResultEntity();
            record.setId(listResults.get(0).getId());
            record.setResultOpt(resultOpt);
            i= examResultEntityMapper.updateByPrimaryKeySelective(record);
        }


        return i;
    }

    int sumbitResult(Long questionnaireId, Long userId,Long pyId,String inqSource,String examType){
        ExamDTO dto = new ExamDTO();
        //dto.setExamId(examId);
        dto.setQuestionnaireId(questionnaireId);
        dto.setUserId(userId);
        dto.setPyId(pyId);
        dto.setInquireSource(inqSource);
        dto.setExamType(examType);
        List<ExamResultEntity> examResultEntity = examResultEntityMapper.selectRecords(dto);





        return  0;
    }

    /**
     * 提交记录
     * @param userId
     * @param questionnaireId
     * @param pyId
     * @param inquireScore
     * @param examType
     * @return
     */
    @Override
    public int selecSubmintRecoreds(Long userId, Long questionnaireId, Long pyId, String inquireScore, String examType) {


        ExamUserStatue record = new ExamUserStatue();
        record.setExamType(examType);
        record.setInquireSource(inquireScore);
        record.setPyId(pyId);
        record.setQuestionnaireId(questionnaireId);
        record.setUserId(userId);
        ExamUserStatue examUserStatue = summintMapper.selectSubmintRecord(record);
        if(ObjectUtils.isBlank(examUserStatue)){
            return  0;
        }

        return 1;
    }

    /**
     * 保存提交记录
     */
    @Override
    public int saveSubmintRecore(Long userId, Long questionnaireId, Long pyId, String inquireScore, String examType) {
        ExamUserStatue record = new ExamUserStatue();
        record.setUserId(userId);
        record.setQuestionnaireId(questionnaireId);
        record.setPyId(pyId);
        record.setInquireSource(inquireScore);
        record.setExamType(examType);
        return summintMapper.insertSelective(record);
    }
}
